<template>
  <div class="article-comments">
    <!-- 评论列表 -->
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell
        v-for="item in commetList"
        :key="item.com_id.toString()"
      >
        <van-image
          slot="icon"
          round
          width="30"
          height="30"
          style="margin-right: 10px;"
          :src="item.aut_photo"
        />
        <span style="color: #466b9d;" slot="title">hello</span>
        <div slot="label">
          <p style="color: #363636;">{{item.content}}</p>
          <p>
            <span style="margin-right: 10px;">{{item.pubdate}}</span>
            <van-button size="mini" type="default">回复</van-button>
          </p>
        </div>
        <van-icon slot="right-icon"
         :name="!item.is_liking ?'like-o':'like'"
         color="red"
         @click="pointLike(item)" />
      </van-cell>
    </van-list>

    <!-- 发布评论 -->
    <van-cell-group class="publish-wrap">
      <van-field
        clearable
        v-model.trim="reviewConent"
        placeholder="请输入评论内容"
      >
        <van-button slot="button" size="mini" type="info" @click="issueConent">发布</van-button>
      </van-field>
    </van-cell-group>
  </div>
</template>

<script>
import { getComments, addComment, addCommentLike, deleteCommentLike } from '@/api/commet'
export default {
  data () {
    return {
      commetList: [], // 页面数据
      offset: null, // 最后一个ID
      loading: false, // 加载
      finished: false, // 所有数据加载完毕
      reviewConent: ''// 评论内容
    }
  },
  methods: {
    // 页面渲染
    loadCommet () {
      // 调用接口
      getComments(this.$route.query.id.toString(), this.offset).then((res) => {
        console.log(res)
        const newCommet = res.data.data.results // 返回真实数据
        this.commetList = [...this.commetList, ...newCommet] // 将新数据添加到老数据后面
        this.loading = false
        // 如果加载完毕了则为TRUE
        if (newCommet.length === 0) {
          this.finished = true
        }
        // 5. 更新offset
        this.offset = res.data.data.last_id
      }).then((err) => {
        console.log(err)
      })
    },
    // 上拉加载
    onLoad () {
      this.loadCommet()
    },
    // 发表文章
    issueConent () {
      // 调用接口
      if (this.reviewConent === '') return
      addComment(this.$route.query.id, this.reviewConent).then((res) => {
        console.log(res)
        this.commetList = [res.data.data.new_obj, ...this.commetList] // 回填真实数据
        this.reviewConent = ''
      }).catch((err) => {
        console.log(err)
      })
    },
    // 评论点赞
    pointLike (item) {
      // 给当前评论点赞
      console.log(item.com_id)
      if (!item.is_liking) {
        // 调用点赞的接口
        addCommentLike(item.com_id.toString()).then((res) => {
          this.$toast.success('点赞成功啦')
        }).catch((err) => {
          console.log(err)
        })
      } else {
      // 给当前评论取消点赞
        deleteCommentLike(item.com_id.toString()).then((res) => {
          this.$toast.fail('取消点赞成功')
        }).catch((err) => {
          console.log(err)
        })
      }
      item.is_liking = !item.is_liking
    }
  }
}
</script>
